﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Kaixin.House.Garden
{
    using Kaixin.Tools;

    public class RemainTime
    {
        public int day;
        public int hour;
        public int min;
        public int sec;

        private string FMT_NODAY = "   {0:D2}:{1:D2}:{2:D2}";
        private string FMT_DAY = "{0:D2}:{1:D2}:{2:D2}:{3:D2}";
        
        public int ToMilliseconds()
        {
            return (((day * 24 + hour) * 60 + min) * 60 + sec) * 1000;
        }

        public bool IsZero()
        {
            return (day==0 && hour ==0 && min ==0 && sec ==0);
        }

        public void SetValue(int d, int h, int m, int s)
        {
            day = d;
            hour = h;
            min = m;
            sec = s;
        }

        public string GetString()
        {
            return ((day == 0) ? string.Format(FMT_NODAY, hour, min, sec) :
                string.Format(FMT_DAY, day, hour, min, sec));
        }
    }

    public class Farm
    {
        private Network _network;

        private string _uid;
        private string _fuid;
        private string _verify;
        private string _sfuid;

        public int FarmNum;
        public int Status = 0;
        public int Shared = 1;
        public int CropsStatus = 0; //-1 --has died; 0 -- not seed; 1 -- growing; 2 -- has grown up; 3 -- unknown status;
        public string Crops;
        public bool Thieved = false;
        public bool Locked = false;
        public int Water;
        public int Grass;
        public int Vermin;
        public string Name;  // seed name
        public string SeedId;
        public int FruitNum;
        public int RemainNum;
        public int Grow;
        public int TotalGrow;
        public RemainTime GrowRemainTime = new RemainTime();
        public RemainTime ThieveWaitTime = new RemainTime();
        public string SFUid // if shared, this is shared friend uid
        {
            get { return _sfuid; }
            set { _sfuid = value; }
        }

        private Friend _friend = null;

        /*
        public Farm(Network network, string uid, string fuid, string verify)
        {
            _network = network;
            _uid = uid;
            _fuid = fuid;
            _verify = verify;
        }
         * */

        public Farm(Network network, string uid, Friend friend, string verify)
        {
            _network = network;
            _uid = uid;
            _fuid = friend.Uid;
            _friend = friend;

            _verify = verify;
        }

        public string GetCropsStatus()
        {
            switch (CropsStatus)
            {
                case -1:
                    return "Died";
                case 0:
                    return "Not seed";
                case 1:
                    return "Growing";
                case 2:
                    return "Grown up";
                default:
                    return "Unknown";
            }
        }

        public bool CanHarvest()
        {
            if (CropsStatus == 2 && ThieveWaitTime.IsZero() && !Thieved)
            {
                if ((Shared != 0) && !_uid.Equals(_sfuid) && 
                    !_uid.Equals(_fuid))
                    return false;

                return true;
            }
            return false;
        }

        public bool CanAntiGrass()
        {
            if (Grass > 0)
                return true;
            else
                return false;
        }

        public bool CanWater()
        {
            if (Water < 5 && Status == 1)
                return true;
            else
                return false;
        }

        public bool CanVermin()
        {
            if (Vermin > 0)
                return true;
            else
                return false;
        }

        public bool CanPlough()
        { 
            if ( ((_uid.Equals(_fuid) && Shared == 0) || (!_uid.Equals(_fuid) && Shared > 0)) &&
               ( ((CropsStatus == 3) || (CropsStatus == -1))  // unkown or die
                && Status == 1) )
                return true;
            else
                return false;
        }

        public bool CanFarmSeed()
        {
            if (((_uid.Equals(_fuid) && Shared == 0) || (!_uid.Equals(_fuid) && Shared > 0)) &&
                (CropsStatus == 0 && Status == 1))
                return true;
            else
                return false;
        }


        public int Harvest(string fuid, string verify)
        {
            string url = Consts.RootURL + Consts.GardenHarvestUrl +
                "?farmnum=" + FarmNum + "&&verify=" + verify + "&fuid=" + fuid + "&seedid=" + SeedId; 

            try 
            {
			    string respXML = _network.CallUrl(url);
			    XMLParser parser = new XMLParser(respXML);
                if ("1".Equals(parser.GetNodeValue("/data/anti")))
                {
                    TextOutput.PrintLine("--- Harvest: AntiTheive !!! ---");
                    //TextOutput.PrintLine("Currently not supported. Please use browser to finish it.");
                    return -1;
                }
                else if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("Harvest: " + _friend.Name + " " 
                        + parser.GetNodeValue("/data/num") + " " + Name + " succ");
                    return 1;
			    } 
                else 
                {
                    TextOutput.PrintLine("Harvest: " + _friend.Name + " "
                        + Name + "Fail. " + parser.GetNodeValue("/data/reason"));
                    return 0;
			    }
		    } 
            catch (Exception) 
            {
                return 0;
		    }
        }

        public bool AntiGrass()
        {
            string url = Consts.RootURL + Consts.GardenAntiGrassUrl +
                "?farmnum=" + FarmNum + "&&verify=" + _verify + "&fuid=" + _fuid + "&seedid=" + SeedId;

            try
            {
                string respXML = _network.CallUrl(url);
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("AntiGrass: " + _friend.Name + " "
                        + "succ");
                    return true;
                }
                else
                {
                    TextOutput.PrintLine("AntiGrass: " + _friend.Name + " "
                        +"Fail " + parser.GetNodeValue("/data/reason"));
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DoWater()
        {
            string url = Consts.RootURL + Consts.GardenWaterUrl +
                "?farmnum=" + FarmNum + "&&verify=" + _verify + "&fuid=" + _fuid + "&seedid=" + SeedId;

            try
            {
                string respXML = _network.CallUrl(url);
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("Water: " + _friend.Name + " "
                        + "succ");
                    return true;
                }
                else
                {
                    TextOutput.PrintLine("Water: " + _friend.Name + " "
                        +"Fail " + parser.GetNodeValue("/data/reason"));
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool AntiVermin()
        {
            string url = Consts.RootURL + Consts.GardenAntiVerminUrl +
                "?farmnum=" + FarmNum + "&&verify=" + _verify + "&fuid=" + _fuid + "&seedid=" + SeedId;

            try
            {
                string respXML = _network.CallUrl(url);
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("AntiVermin: " + _friend.Name + " "
                        + "succ");
                    return true;
                }
                else
                {
                    TextOutput.PrintLine("AntiVermin: " + _friend.Name + " "
                        + "Fail " + parser.GetNodeValue("/data/reason"));
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }


        public bool Plough() // check first
        {
            string url = Consts.RootURL + Consts.GardenPloughUrl +
                "?farmnum=" + FarmNum + "&&verify=" + _verify + "&seedid=0&fuid=" + _fuid;

            try
            {
                string respXML = _network.CallUrl(url);
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("Plough: " + _friend.Name + " "
                        + "succ");
                    return true;
                }
                else
                {
                    TextOutput.PrintLine("Plough: " + _friend.Name + " "
                        + "Fail " + parser.GetNodeValue("/data/reason"));
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool FarmSeed(string seedid)
        {
            string url = Consts.RootURL + Consts.GardenFarmSeedUrl +
                "?farmnum=" + FarmNum + "&&verify=" + _verify + "&fuid=" + _fuid + "&seedid=" + seedid;

            try
            {
                string respXML = _network.CallUrl(url);
                XMLParser parser = new XMLParser(respXML);
                if ("succ".Equals(parser.GetNodeValue("/data/ret")))
                {
                    TextOutput.PrintLine("FarmSeed: " + _friend.Name + " "
                        + "succ " + SeedList.FindSeedName(seedid));
                    return true;
                }
                else
                {
                    TextOutput.PrintLine("FarmSeed: " + _friend.Name + " "
                        + "Fail. ID=" + FarmNum + " Reason " + parser.GetNodeValue("/data/reason"));
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
